﻿using CDSysLDManageProject.BYDTableAdapters;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace CDSysLDManageProject
{
    class BYDMesDAL
    {



        tbl_DCIRDataTableAdapter dcir = new tbl_DCIRDataTableAdapter();
        T_MESResultMessageTableTableAdapter mesmsg = new T_MESResultMessageTableTableAdapter();
        T_OCVResultDataTableAdapter ocvresult = new T_OCVResultDataTableAdapter();
        tb_DCIRRecipeInfoTableAdapter recipeinfo = new tb_DCIRRecipeInfoTableAdapter();
        int allowngcount = 3;
        string c15recipeid = "";
        string c19recipeid = "";
        /// <summary>
        /// 通过托盘号获取当前托盘号上的电池信息测试用
        /// </summary>
        /// <param name="TrayId">托盘号</param>
        /// <returns></returns>
        public string GetCellInfoByTrayIdTest(DataTable dtcellinfo)
        {
           
            string cellno = "";
            for (int i = 0; i < dtcellinfo.Rows.Count; i++)
            {
                cellno = cellno + dtcellinfo.Rows[i]["cellno"].ToString().PadRight(20, ' ');
            }
            return cellno;



        }


        /// <summary>
        /// 通过托盘号获取当前托盘号上的电池信息
        /// </summary>
        /// <param name="dtcellinfo">电池信息</param>
        /// <returns></returns>
        public string GetCellStsTest(DataTable dtcellinfo)
        {
            string cellsts = "";

            for (int i = 0; i < dtcellinfo.Rows.Count; i++)
            {//向前拼电池状态
                cellsts = cellsts+dtcellinfo.Rows[i]["cellsts"].ToString();
            }
            return cellsts;

        }


        /// <summary>
        /// 通过托盘号获取当前托盘号上的电池信息
        /// </summary>
        /// <param name="dtcellinfo">电池信息</param>
        /// <returns></returns>
        public string GetCellStsTest1(DataTable dtcellinfo)
        {
            string cellsts = "";

            for (int i = 0; i < dtcellinfo.Rows.Count; i++)
            {//向前拼电池状态
                if (string.IsNullOrEmpty(dtcellinfo.Rows[i]["cellno"].ToString()))
                {
                    cellsts = cellsts + "06";
                }
                else
                {
                    cellsts = cellsts + "00";
                }
                
            }
            return cellsts;

        }


        /// <summary>
        /// 通过托盘号获取当前托盘号上的电池信息
        /// </summary>
        /// <param name="TrayId">托盘号</param>
        /// <returns></returns>
        public string GetCellInfoByTrayId(string TrayId)
        {
            T_TrayCellDataTableAdapter traycell = new T_TrayCellDataTableAdapter();
            string cellno = "";
            DataTable dtcellno = new DataTable();
            int allowngcount = 3;
            int ngcount = 0;

            try
            {
                
                dtcellno = traycell.GetCellNoByTrayNo(TrayId);
                
            }
            catch (Exception ex)
            {

                Log.SystemError.Add("根据托盘号获取电池编号失败，可能是由于网络原因导致的" + ex.ToString());
                ngcount++;
            }

            while (dtcellno.Rows.Count <= 0 && ngcount <= allowngcount)
            {
                try
                {
                    
                    dtcellno = traycell.GetCellNoByTrayNo(TrayId);
                    ngcount++;
                }
                catch (Exception ex)
                {

                    Log.SystemError.Add("根据托盘号获取失败，可能是由于网络原因导致的"+ex.ToString());
                    ngcount++;
                }
                
            }
            for (int i = 0; i < dtcellno.Rows.Count; i++)
            {
                cellno = cellno + dtcellno.Rows[i]["CELL_NO"].ToString().PadRight(20, ' ');
            }
            return cellno;
            

                
        }

       
        /// <summary>
        /// 获取指定架台位置堆垛机叉子状态
        /// </summary>
        /// <param name="stageLoc">架台位置编号</param>
        /// <returns></returns>
        public string GetLDForkStByStageLoc(string stageLoc)
        {
            return "1";//0 : Fork MOVE, 1: Fork Home
        }


        public string GetRecipeByTrayType(string trayno)
        {
            string traytype = trayno.Substring(trayno.Length - 3);
            DataTable dtrecipeinfo =new DataTable();
            int ngcount=0;
            try 
	        {	        
		        dtrecipeinfo = recipeinfo.GetRecipeInfoByTrayType(traytype);
                ngcount++;
	        }
	        catch (Exception ex)
	        {
		        Log.SystemError.Add("根据托盘类型查询工艺号失败"+ex.ToString());
                ngcount++;
	        }
            
            if (dtrecipeinfo.Rows.Count > 0)
            {
                if (traytype == "C19")
                {
                    c19recipeid = dtrecipeinfo.Rows[0]["recipeid"].ToString();
                }
                else if (traytype == "C15" || traytype == "C12")
                {
                    c15recipeid = dtrecipeinfo.Rows[0]["recipeid"].ToString();
                }

                return dtrecipeinfo.Rows[0]["recipeid"].ToString();
            }
            else
            {
                while(dtrecipeinfo.Rows.Count<=0 &&ngcount<=allowngcount)
                {
                    try
                    {
                        dtrecipeinfo = recipeinfo.GetRecipeInfoByTrayType(traytype);
                        ngcount++;
                    }
                    catch (Exception ex)
                    {
                        Log.SystemError.Add("根据托盘类型查询工艺号失败" + ex.ToString());
                        ngcount++;
                    }
                }    
            }
            if (dtrecipeinfo.Rows.Count > 0)
            {
                if (traytype == "C19")
                {
                    c19recipeid = dtrecipeinfo.Rows[0]["recipeid"].ToString();
                }
                else if (traytype == "C15" || traytype == "C12")
                {
                    c15recipeid = dtrecipeinfo.Rows[0]["recipeid"].ToString();
                }
                return dtrecipeinfo.Rows[0]["recipeid"].ToString();
            }
            else
            {   
            }

            if (traytype == "C19")
            {
                return c19recipeid;
            }
            else 
            {
                return c15recipeid;
            }
            
            


        }

        /// <summary>
        /// 保存充放电结果
        /// </summary>
        /// <param name="cdResult"></param>
        /// <returns></returns>
        public bool InsertStepResult(CDStepResult cdResult,DateTime dcirendtime)
        {
           
            double Voltage = 0;
            double Current = 0;
            double DOCVVlotage = 0;
            double DCIRDifference = 0;
            double DCIR = 0;
            double ChargeVoltage = 0;
            double ChargeCurrent = 0;
            DateTime endtime = dcirendtime;
            if (string.IsNullOrEmpty(cdResult.Voltage.ToString().Replace(" ", "")))
            {

            }
            else
            {
                Voltage = double.Parse(cdResult.Voltage.ToString()) / 1000;
            }

            if (string.IsNullOrEmpty(cdResult.Current.ToString().Replace(" ", "")))
            {

            }
            else
            {
                Current = double.Parse(cdResult.Current.ToString()) / 1000;
            }

            if (string.IsNullOrEmpty(cdResult.DOCVVlotage.ToString().Replace(" ", "")))
            {

            }
            else
            {
                DOCVVlotage = double.Parse(cdResult.DOCVVlotage.ToString()) / 1000;
            }

            if (string.IsNullOrEmpty(cdResult.DCIRDifference.ToString().Replace(" ", "")))
            {

            }
            else
            {
                DCIRDifference = double.Parse(cdResult.DCIRDifference.ToString());
            }

            if (string.IsNullOrEmpty(cdResult.DCIR.ToString().Replace(" ", "")))
            {

            }
            else
            {
                DCIR = double.Parse(cdResult.DCIR.ToString());
            }

            if (string.IsNullOrEmpty(cdResult.ChargeVoltage.ToString().Replace(" ", "")))
            {

            }
            else
            {
                ChargeVoltage = double.Parse(cdResult.ChargeVoltage.ToString());
            }

            if (string.IsNullOrEmpty(cdResult.ChargeCurrent.ToString().Replace(" ", "")))
            {

            }
            else
            {
                ChargeCurrent = double.Parse(cdResult.ChargeCurrent.ToString());
            }

            try
            {
                int cellcount = int.Parse(dcir.GetCellCountByCellTrayEndTime(cdResult.TrayID, cdResult.CellID, cdResult.EndTime).ToString());
                if (cellcount > 0)
                {
                    try
                    {
                        dcir.DeleteDataByTrayCellEndTime(cdResult.TrayID, cdResult.CellID, cdResult.EndTime);
                    }
                    catch (Exception ex)
                    {
                        Log.SQL.Add("在神亚数据表删除" + cdResult.TrayID + "托盘的第" + cdResult.CellCH + "通道的" + cdResult.CellID + "的DCIR数据失败!" + ex);
                    }

                    try
                    {
                        dcir.InsertDCIRData(cdResult.StageLoc, cdResult.TrayID, int.Parse(cdResult.CellCH), cdResult.CellID, cdResult.RecipeID, cdResult.StartTime, cdResult.EndTime, Decimal.Parse(Voltage.ToString()), Decimal.Parse(Current.ToString()), Decimal.Parse(DOCVVlotage.ToString()), Decimal.Parse(DCIRDifference.ToString()), Decimal.Parse(DCIR.ToString()), int.Parse(cdResult.CellStatus.ToString()), Decimal.Parse(ChargeVoltage.ToString()), Decimal.Parse(ChargeCurrent.ToString()));
                    }
                    catch (Exception ex)
                    {
                        Log.SQL.Add("将" + cdResult.TrayID + "托盘的第" + cdResult.CellCH + "通道的" + cdResult.CellID + "的DCIR数据插入到神亚数据表失败!" + ex);
                    }
                }
                else
                {
                    try

                    {
                        dcir.InsertDCIRData(cdResult.StageLoc, cdResult.TrayID, int.Parse(cdResult.CellCH), cdResult.CellID, cdResult.RecipeID, cdResult.StartTime, cdResult.EndTime, Decimal.Parse(Voltage.ToString()), Decimal.Parse(Current.ToString()), Decimal.Parse(DOCVVlotage.ToString()), Decimal.Parse(DCIRDifference.ToString()), Decimal.Parse(DCIR.ToString()), int.Parse(cdResult.CellStatus.ToString()), Decimal.Parse(ChargeVoltage.ToString()), Decimal.Parse(ChargeCurrent.ToString()));
                    }
                    catch (Exception ex)
                    {
                        Log.SQL.Add("将" + cdResult.TrayID + "托盘的第" + cdResult.CellCH + "通道的" + cdResult.CellID + "的DCIR数据插入到神亚数据表失败!" + ex);
                    }

                }
            }
            catch (Exception ex)
            {
                Log.SQL.Add("将" + cdResult.TrayID + "托盘的第" + cdResult.CellCH + "通道的" + cdResult.CellID + "的DCIR数据插入到神亚数据表失败!" + ex);
            }
            ////将flag=8的DCIR数据插入到MES中间表
            try
            {
                mesmsg.InsertMESDCIRData(cdResult.TrayID, cdResult.EndTime, cdResult.CellID, int.Parse(cdResult.CellCH), null, null, null, null, null, null, null, null, null, null, null, null, null, null, endtime, Double.Parse(DCIR.ToString()), null, null, null, null, 8);
            }
            catch (Exception ex)
            {
                Log.SQL.Add("将" + cdResult.TrayID + "托盘的第" + cdResult.CellCH + "通道的" + cdResult.CellID + "的DCIR数据插入到MES中间表失败!" + ex);
            }


            return true;
        }
    }
}
